package com.anjiplus.template.gaea.business.config;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.annotation.NacosProperties;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.utils.NetUtils;
import com.alibaba.nacos.spring.context.annotation.discovery.EnableNacosDiscovery;
import com.anjiplus.template.gaea.business.ReportApplication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.stereotype.Component;

@Component
@EnableNacosDiscovery(globalProperties = @NacosProperties(serverAddr = "mse-657bc740-nacos-ans.mse.aliyuncs.com:8848",namespace = "sass-prod"))
public class NacosConfig implements CommandLineRunner {
    @NacosInjected
    private NamingService namingService;
    @Autowired
    private ServerProperties serverProperties;
    private static final Logger logger = LoggerFactory.getLogger(ReportApplication.class);

    @Override
    public void run(String... args) throws Exception {
        try {
            Integer port = serverProperties.getPort();
            Instance instance = new Instance();
            instance.setIp(NetUtils.localIP());
            instance.setPort(port);
            instance.setServiceName("bsReportBI");
            this.namingService.registerInstance("bsReportBI","sass-prod", instance);
            logger.info("Finished auto register service : {}, ip : {}, port : {}", new Object[]{"bsReportBI", instance.getIp(), instance.getPort()});
        } catch (NacosException var5) {
            var5.printStackTrace();
        }
    }

}
